﻿using System;
using System.Collections.Generic;
using System.Text;
using Csla;
using System.Data.Odbc;
using MinaspetroLibrary.Configuracoes;
using System.Data;
using Csla.Data;

namespace MinaspetroLibrary.CurriculoPack
{

    [Serializable()]
    public class CursoListInfo : ReadOnlyListBase<CursoListInfo, CursoChildInfo>
	{
		#region Authorization Rules

        public static bool CanGetObject()
        {
            return true;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private CursoListInfo()
        { /* require use of factory method */ }

        public static CursoListInfo GetCursoListInfo()
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("Usuário não autorizado a visualizar CursoListInfo");
            return DataPortal.Fetch<CursoListInfo>(new FilterCriteria());
        }

        public static CursoListInfo GetCursoListInfo(Guid idCurriculo)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("Usuário não autorizado a visualizar CursoListInfo");
            return DataPortal.Fetch<CursoListInfo>(new FilterCriteria(idCurriculo));
        }

        public static CursoListInfo GetCursoListInfo(int indexInicio, int quantidade)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("Usuário não autorizado a visualizar CursoListInfo");
            return DataPortal.Fetch<CursoListInfo>(new FilterCriteria(indexInicio, quantidade));
        }
		
        #endregion //Factory Methods

        #region Data Access

        #region Filter Criteria
        [Serializable()]
        private class FilterCriteria
        {
            public int IndexInicio = 0;
            public int Quantidade = 0;
            public Guid IdCurriculo = Guid.Empty;

            public FilterCriteria(int indexInicio, int quantidade)
            {
                this.IndexInicio = indexInicio;
                this.Quantidade = quantidade;
            }

            public FilterCriteria()
            {
            }

            public FilterCriteria(Guid idCurriculo)
            {
                this.IdCurriculo = idCurriculo;
            }

        }
        #endregion //Filter Criteria

        #region Data Access - Fetch
        private void DataPortal_Fetch(FilterCriteria criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            using (OdbcConnection cn = new OdbcConnection(ConfigDatabase.MinaspetroConnection))
            {
                cn.Open();
                ExecuteFetch(cn, criteria);
            }//using

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        private void ExecuteFetch(OdbcConnection cn, FilterCriteria criteria)
        {
            using (OdbcCommand cm = cn.CreateCommand())
            {
                cm.CommandType = CommandType.Text;
                cm.CommandText = GetFetchScript(criteria);

                using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                {
                    while (dr.Read())
                        this.Add(CursoChildInfo.GetCursoChildInfo(dr));
                }
            }//using
        }

        private string GetFetchScript(FilterCriteria criteria)
        {
            StringBuilder strQuery = new StringBuilder();
            StringBuilder limit = new StringBuilder();

            strQuery.Append(" SELECT Id, IdCurriculo, Descricao, Cidade, CargaHora, Instituicao, DataInicio, DataFim ");
            strQuery.Append(" FROM cursotb WHERE 1=1 ");
            if (!criteria.IdCurriculo.Equals(Guid.Empty))
                strQuery.Append(" AND IdCurriculo = \"" + criteria.IdCurriculo.ToString() + "\" ");
            strQuery.Append(" ORDER BY DataInicio, IdCurriculo ");
            if (criteria.Quantidade != 0)
				strQuery.Append(" LIMIT " + criteria.IndexInicio + "," + criteria.Quantidade + ";");
            return strQuery.ToString();
        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
